<?php
namespace app\scodetrace\controller;

class Batches extends \app\common\controller\Base {
	protected $modelname='Batch';
	protected $autoWriteUserId=true;
	use \app\common\controller\BaseLog;

	protected function _index(){
		$condition=[];
		$param=$this->param;

    if (isset($param['serial_number']))
      $condition[]=['serial_number','=',$param['serial_number']];

    if (isset($param['name']))
      $condition[]=['name','like',"%".$param['name']."%"];

    if (isset($param['create_time']))
      $condition[]=['create_time','between',$param['create_time']];

		if (isset($param['out_time']))
      $condition[]=['out_time','between',$param['out_time']];

    if (isset($param['product_no']))
      $condition[]=['product_id','in',model('baseinfo/Product')->where('serial_number',"%".$param['product_no']."%")->column("id")];

    if (isset($param['area'])){
      $m = model('baseinfo/Area')->where('name','like',"%".$param['area']."%")->column("id");
      if (count($m)) {
        $arr = array_unique(array_merge(model('baseinfo/Area')->getAllChild($m),$m));
        $condition[]=['area_id','in',$arr];
      }
    }

		if (isset($param['expire_time']))
      $condition[]=['expire_time','between',$param['expire_time']];

    if (isset($param['submitter_time']))
      $condition[]=['submitter_time','between',$param['submitter_time']];

		if (isset($param['submitter']))
      $condition[]=['submitter_id',"in",model('admin/AdminUser')->where('real_name','like','%'.$param['submitter'].'%')->column("id")];

    // if (isset($param['code_type']))
    //   $condition[]=['code_type','between',$param['query_amount'];
    if (isset($param['deleted']))
      $condition[]=['deleted','=',$param['deleted']=='true'?'1':'0'];
    else
      $condition[]=['deleted','=','0'];

		return $condition;
	}

  protected function _order(&$field) {
    if ($field=='serial_number') {
      return true;
    }
    return false;
  }

  public function getLog() {
    if (!($id=$this->param('id',null))) {
      return json_pack('必须指定批次id',400);
    }
    if ($m=model($this->modelname)->get($id)) {
      return json_pack($m->logs()->visible(['logtext','area','result','submitter','submitter_time','remark']));
    } else {
      return json_pack('找不到指定的批次',400);
    }
  }

  public function logOne() {
    if (!($id=$this->param('id',null))) {
      return json_pack('必须指定批次id',400);
    }
    if (!($logtext=$this->param('logtext',null))){
      return json_pack('log内容不能为空',400);
    }
    if (!($area=$this->param('area',null))){
      return json_pack('操作地点不能为空',400);
    }
    if (!($result=$this->param('result',null))){
      return json_pack('操作结果不能为空',400);
    }

    if ($m=model($this->modelname)->get($id)) {
      if ($m->logOne($logtext,$area,$result)) {
        return json_pack('OK');
      } else {
        return json_pack($m->getError(),400);
      }
    } else {
      return json_pack('找不到指定的批次',400);
    }
    if (!($area=$this->param(''))){
    }
  }
}

?>